<template>
  <view>
    <!-- 用户开始 想写一起的 到时看下有没不对-->
    <view class="user_wrap">
      <view class="user_img">
        <image :src="imgDetailList.user.avatar" mode="widthFix" />
      </view>
      <view class="user_info">
        <view class="user_name">{{imgDetailList.user.name}}</view>
        <view class="user_atime">{{imgDetailList.cnTime}}</view>
      </view>
    </view>
    <!-- 用户结束 -->

    <!-- 图片开始 -->
    <view class="img_wrap">
      <swiper-action @swiperAction="handleSwiperAction">
        <image :src="imgDetailList.thumb" />
      </swiper-action>
    </view>
    <!-- 图片结束 -->

    <!-- 点赞收藏开始 -->
    <view class="landc_wrap">
      <view class="img_like">
        <text class="iconfont icondianzan">{{imgDetailList.rank}}</text>
      </view>
      <view class="img_collection">
        <text class="iconfont iconshoucang">收藏</text>
      </view>
    </view>
    <!-- 点赞收藏结束 -->

    <!-- 专辑开始 -->
    <view class="album_wrap" v-if="album.length">
      <view class="album_title">
        <text>相关</text>
      </view>
      <view class="album_list">
        <view class="album_item" v-for="item in album" :key="item.id">
          <view class="album_img">
            <image :src="item.cover" mode="aspectFill" />
          </view>
          <view class="album_detail">
            <view class="album_btn">专辑</view>
            <view class="album_name">{{item.name}}</view>
            <view class="iconfont iconiconfontjiantou4"></view>
          </view>
        </view>
      </view>
    </view>
    <!-- 专辑结束 -->

    <!-- 最热评论开始 -->
    <view class="comment hot" v-if="hotC.length">
      <!-- 标题 -->
      <view class="comment_title">
        <!-- 图标 -->
        <text class="iconfont iconhot1"></text>
        <!-- 文本 -->
        <text class="title_text">最热评论</text>
      </view>
      <!-- 遍历 -->
      <view class="comment_list">
        <view class="comment_items" v-for="item in hotC" :key="item.id">
          <!-- 用户信息 -->
          <view class="comment_user">
            <!-- 头像 -->
            <view class="user_img">
              <image :src="item.user.avatar" mode="widthFix" />
            </view>
            <!-- 用户详细 -->
            <view class="user_info">
              <view class="user_name">{{item.user.name}}</view>
              <view class="user_time">{{item.cnTime}}</view>
            </view>
            <!-- 用户徽章 -->
            <view class="user_badge">
              <image v-for="item2 in item.user.title" :key="item2.icon" :src="item2.icon" />
            </view>
          </view>
          <!-- 评论数据 -->
          <view class="comment_desc">
            <view class="comment_content">{{item.content}}</view>
            <view class="comment_like">
              <text class="iconfont icondianzan">{{item.size}}</text>
            </view>
          </view>
        </view>
      </view>
    </view>
    <!-- 最热评论结束 -->

    <!-- 最新评论开始 -->
    <view class="comment new" v-if="newC.length">
      <!-- 标题 -->
      <view class="comment_title">
        <!-- 图标 -->
        <text class="iconfont iconpinglun"></text>
        <!-- 文本 -->
        <text class="title_text">最新评论</text>
      </view>
      <!-- 遍历 -->
      <view class="comment_list">
        <view class="comment_items" v-for="item in newC" :key="item.id">
          <!-- 用户信息 -->
          <view class="comment_user">
            <!-- 头像 -->
            <view class="user_img">
              <image :src="item.user.avatar" mode="widthFix" />
            </view>
            <!-- 用户详细 -->
            <view class="user_info">
              <view class="user_name">{{item.user.name}}</view>
              <view class="user_time">{{item.cnTime}}</view>
            </view>
            <!-- 用户徽章 -->
            <view class="user_badge">
              <image v-for="item2 in item.user.title" :key="item2.icon" :src="item2.icon" />
            </view>
          </view>
          <!-- 评论数据 -->
          <view class="comment_desc">
            <view class="comment_content">{{item.content}}</view>
            <view class="comment_like">
              <text class="iconfont icondianzan">{{item.size}}</text>
            </view>
          </view>
        </view>
      </view>
    </view>
    <!-- 最新评论结束 -->

    <!-- 下载开始 -->
    <view class="download_wrap">
      <view class="download" @click="handleDownload">下载图片</view>
    </view>
    <!-- 下载结束 -->
  </view>
</template>

<script>
import moment from "moment";
moment.locale("zh-cn");
import swiperAction from "@/component/swiperAction";
export default {
  components: { swiperAction },
  data() {
    return {
      imgDetailList: {},
      album: [],
      hotC: [],
      newC: [],
      imgIndex: 0
    };
  },
  methods: {
    getComment(id) {
      this.request({
        url: `http://157.122.54.189:9088/image/v2/wallpaper/wallpaper/${id}/comment`
      }).then(res => {
        //console.log(res.res);
        this.album = res.res.album;

        res.res.hot.forEach(v => (v.cnTime = moment(v.atime * 1000).fromNow()));
        res.res.comment.forEach(
          v => (v.cnTime = moment(v.atime * 1000).fromNow())
        );

        this.hotC = res.res.hot;
        this.newC = res.res.comment;
      });
    },
    handleSwiperAction(e) {
      // console.log(e.direction);
      const { imgList } = getApp().globalData;
      if (e.direction === "right" && this.imgIndex > 0) {
        this.imgIndex--;
        this.getData();
      } else if (e.direction === "left" && this.imgIndex < imgList.length - 1) {
        this.imgIndex++;
        this.getData();
      } else {
        uni.showToast({
          title: "不要再滑啦！！没有数据了！",
          icon: "none"
        });
      }
    },
    getData() {
      const { imgList } = getApp().globalData;
      this.imgDetailList = imgList[this.imgIndex];
      // 拼接图片src
      // this.imgDetailList.newThumb =
      //   this.imgDetailList.thumb +
      //   this.imgDetailList.rule.replace("$<Height>", 360);
      //console.log(this.imgDetailList);

      //时间戳
      this.imgDetailList.cnTime = moment(
        this.imgDetailList.atime * 1000
      ).fromNow();
      //评论
      this.getComment(this.imgDetailList.id);
    },
    async handleDownload() {
      await uni.showLoading({
        title: "下载中"
      });

      const result1 = await uni.downloadFile({ url: this.imgDetailList.img });
      const { tempFilePath } = result1[1];

      const result2 = await uni.saveImageToPhotosAlbum({ filePath: tempFilePath });

      uni.hideLoading();

      await uni.showToast({ title: "下载成功!" });
    }
  },
  onLoad() {
    //console.log(getApp().globalData);
    // this.imgDetailList = getApp().globalData.list[getApp().globalData.index];
    const { imgIndex } = getApp().globalData;
    this.imgIndex = imgIndex;
    this.getData();
  }
};
</script>

<style lang="scss" scoped>
.user_wrap {
  padding: 20rpx;
  display: flex;
  .user_img {
    padding: 0 20rpx;
    image {
      width: 80rpx;
      border-radius: 50%;
    }
  }
  .user_info {
    .user_name {
      font-size: 30rpx;
      color: #000;
      font-weight: 600;
    }
    .user_atime {
      font-size: 24rpx;
      color: #ccc;
      padding: 10rpx 0;
    }
  }
}

.img_wrap {
  image {
    border: 2rpx pink solid;
    border-bottom: 1px solid #ccc;
  }
}

.landc_wrap {
  display: flex;
  justify-content: space-between;
  border-bottom: 1px solid #ccc;
  padding: 0 20rpx;
  height: 80rpx;
  .img_like {
    display: flex;
    justify-content: center;
    align-items: center;
    flex: 1;
  }
  .img_collection {
    display: flex;
    justify-content: center;
    align-items: center;
    flex: 1;
  }
}

.album_wrap {
  padding: 20rpx;
  border-bottom: 3px solid #ccc;
  .album_title {
    border-left: solid $color 5rpx;
    text {
      font-size: 35rpx;
      padding-left: 10rpx;
    }
  }
  .album_item {
    display: flex;
    .album_img {
      padding: 10rpx;
      flex: 1;
      image {
        width: 180rpx;
        height: 180rpx;
        border-radius: 40%;
      }
    }
    .album_detail {
      position: relative;
      flex: 3;
      padding-left: 10rpx;
      .album_btn {
        background-color: $color;
        width: 100rpx;
        height: 50rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #fff;
      }
      .album_name {
        color: #ccc;
        padding: 10rpx 0;
      }
      .iconfont {
        position: absolute;
        top: 50%;
        font-size: 40rpx;
        transform: translateY(-50%);
        right: 10%;
        color: $color;
      }
    }
  }
}

.comment {
  .comment_title {
    padding: 10rpx;
    .iconfont {
      color: red;
      font-size: 40rpx;
    }
    .title_text {
      padding-left: 10rpx;
      color: #000;
      font-size: 30rpx;
      font-weight: 600;
    }
  }
  .comment_list {
    .comment_items {
      padding: 5rpx 0;
      border-bottom: 2px solid #ccc;
      .comment_user {
        display: flex;
        .user_img {
          padding: 10rpx;
          display: flex;
          justify-content: center;
          align-items: center;
          width: 15%;
          image {
            width: 90%;
            border-radius: 10rpx;
          }
        }
        .user_info {
          flex: 1;
          .user_name {
            color: #000;
            font-size: 28rpx;
          }
          .user_time {
            color: #ccc;
            font-size: 24rpx;
          }
        }
        .user_badge {
          image {
            height: 40rpx;
            width: 40rpx;
            display: inline-block; //行内
          }
        }
      }
      .comment_desc {
        display: flex;
        .comment_content {
          padding-left: 15%;
          flex: 1;
          font-size: 28rpx;
        }
        .comment_like {
          .iconfont {
          }
        }
      }
    }
  }
}

.new {
  .iconpinglun {
    color: aqua !important;
  }
}

.download_wrap {
  height: 120rpx;
  justify-content: center;
  align-items: center;
  display: flex;
  .download {
    width: 90%;
    height: 85%;
    background-color: $color;
    color: #fff;
    font-size: 50rpx;
    font-weight: 600;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>